package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravlesService;
import cn.wolfcode.luowowo.article.vo.StrategyPersistenceVo;
import cn.wolfcode.luowowo.article.vo.TravelPersistenceVo;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcoe.luowowo.common.util.MyBeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

//@Component
public class RedisDataPersistenceJob {
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelStatisVOService travelStatisVOService;
    @Reference
    private ITravlesService travlesService;

    //注意是6位的
    @Scheduled(cron = "0/10 * * * * ? ")
    public void StrategyStatisVOPersistence() {
        //先将数据从redis中查询出来
        String pattern = RedisKeys.STRATEGY_STATIS_VO.join( "*" );
        List <StrategyStatisVO> list = strategyStatisVOService.getStrategyStatisVOKeys( pattern );
        //将vo对象转化成攻略的对象,更新到数据库中
        StrategyPersistenceVo spvo = new StrategyPersistenceVo();
        for (StrategyStatisVO vo : list) {
            MyBeanUtil.copyProperties( spvo, vo );
            strategyDetailService.updateStatisVo( spvo );
        }

        //排行的数据没有必要持久到数据库中

    }

    @Scheduled(cron = "0/10 * * * * ? ")
    public void TravelStatisVOPersistence() {
        //先将数据从redis中查询出来
        String pattern = RedisKeys.TRAVEL_STATIS_VO.join( "*" );
        List <TravelStatisVO> list = travelStatisVOService.getTravelStatisVOKeys( pattern );
        //将vo对象转化成攻略的对象,更新到数据库中
        TravelPersistenceVo spvo = new TravelPersistenceVo();
        for (TravelStatisVO vo : list) {
            MyBeanUtil.copyProperties( spvo, vo );
            travlesService.updateStatisVo( spvo );
        }

    }
}
